<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Template_a extends CI_Controller {

        public function __construct()
        {
            parent::__construct();
        }
	public function index()
	{
//                $this->load->helper('html');
//                $this->load->view("template_u/template_u_header");
                $this->admin("login");
//                $this->load->view("template_u/template_u_footer");
	}
        public function admin($file,$value=0,$value1=1,$value2=0)
        {
            $this->load->helper('html');
            $this->load->helper('date');     
            $this->load->helper('string');
            $this->load->view("template_a/template_a_header");    
            $data['select_menu']    = $value;
            $data['access_admin']   =   false;
            $perpage    =   $data['perpage']        =   20;
//            $data['key']       = $value;             
//            echo "file: ".$file;            
            if($file=="checkadmin")
            {
                if(isset($_POST['username']) && isset($_POST['password'])){
                    $rs    = $this->homepage_model->getCheckAdmin($_POST['username'],$_POST['password']);
                    $data["r"]     = $rs->result_array();
                    $data["r_num"] = $rs->num_rows();
                    $data['user_in']   =   $_POST['username'];
                    $data['pass_in']   =   $_POST['password'];
                    $data['access_admin']   =   true;
                    $this->load->view("backend/admin/$file",$data);
                }else{
                    $this->load->view("backend/admin/login");
                }
            }
            else 
            if($file=="admin")
            {   
                if($value=="show"){
                    
                        $row_last   = $value1*$perpage;
                        $row_first  = $row_last-$perpage;                        
                        
                        $rs_all = $this->homepage_model->getNews_all();
                        $data['r_all']      = $rs_all->result_array();
                        $data['r_num_all']  = $rs_all->num_rows();                        
                        
                        if($data['r_num_all']<$row_last){
                            $row_last = $data['r_num_all'];
                        }
                        
                        $rs = $this->homepage_model->getNews($row_first,$perpage);                                                
                        $data['r']      = $rs->result_array();
                        $data['r_num']  = $rs->num_rows();
                        
                        $data['row_last']       =  $row_last;
                        $data['row_first']       =  $row_first;
                        
//                        echo "first: ".$row_first." , last: ".$row_last;
                        $this->load->view("backend/admin/$file",$data);
                        
                }else if($value=="add"){
                    
                    $this->load->view("backend/admin/$file",$data);
                    
                }else if($value=="add_news"){
//                $data['key']       = $value;
//                $data['select_menu']    = $value1;                    
//                    $this->load->library('upload');
                    $headnews   =   $_POST['headnews'];
//                    $headline   =   $_POST['headline'];
                    $detail     =   $_POST['detail'];
                    $linkurl    =   $_POST['linkurl'];
                    if($linkurl==""){
                        $linkurl    = "http://www.watsala.com";
                    }
                    $pic_news   =   $_FILES['userfile']['name'];
//                    echo "userfile: ".$_FILES['userfile']['name'];
//                    $data['pic_news']   =   $_POST['pic_news'];

		$config['upload_path'] = './assets/images/news';
                $config['allowed_types'] = 'jpg|jpeg|gif|png';
		$config['max_size']	= '100';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';
                $config['overwrite'] = TRUE;            

                if($_FILES['userfile']['name']==""){
                    $pic_news   = "news_img.jpg";
                }else{
                    $this->load->library('upload', $config);

                    if ( ! $this->upload->do_upload())
                    {
                            $data['error'] = array('error' => $this->upload->display_errors());
                            $data['select_menu']    =   "add_error";
                            $this->load->view('backend/admin/admin', $data);
                            exit();
                    }
                    else
                    {
                            $data['upload_data']    =   $this->upload->data();
                    }
                }
                        $rs = $this->db->query("SELECT id_new FROM NEWS ORDER BY ID_NEW DESC LIMIT 1");
                        $id_new     = $rs->result_array();
                        foreach($id_new as $max_id){
                            $mx = $max_id['id_new'];
                            $mx = (int)$mx+1;
                            $id_new = sprintf("%010d", $mx);
                        }
                        $data['select_menu']    =   "add_success";
                        
                        $i=0;
                        $this->db->set('id_new', $id_new);
                        $this->db->set('id_temple', '0010');
                        $this->db->set('headnews', $headnews);
//                        $this->db->set('headline', $headline);
                        $this->db->set('detail', $detail);
                        $this->db->set('pic_news', $pic_news);
                        $this->db->set('date', date('Y-m-d'));
                        $this->db->set('linkurl', $linkurl);
                        $this->db->insert('news');

			$this->load->view('backend/admin/admin', $data);
                        
                }else if($value=="update"){
                    $this->load->view("backend/admin/$file",$data);                        
                }
          
            }else if($file=="login")
            {                
                $this->load->view("backend/admin/$file");                
            }
            else 
            if($file=="logout")
            {

                $this->load->view("backend/admin/$file");                
            }
            $this->load->view("template_a/template_a_footer");            
        }
}